﻿using LD.ZKT;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmPwdChange : Form
    {
        private string _name = string.Empty;
        private string TouchId = string.Empty;
        private UserModel _user;
        public FrmPwdChange()
        {
            InitializeComponent();
        }

        public FrmPwdChange(string name) : this()
        {
            _name = name;
            this.txt_userName.Text = name;
            _user = UserHelper.Instance.GetUsers.Where(p => p.Name == _name).FirstOrDefault();
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            ZKTHelper.Instance.ImageChangedEvent += ImageChangedEvent;
            ZKTHelper.Instance.MessageChangeEvent += MessageChangeEvent;
        }

        private void btn_enabled_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_userName.Text) ||
                string.IsNullOrEmpty(this.txt_pwd1.Text) ||
                string.IsNullOrEmpty(this.txt_pwd2.Text))
                return;
            if (_user == null)
            {
                MessageBox.Show("账户不存在", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.txt_pwdOld.Text != _user.Pwd)
            {
                MessageBox.Show("旧密码录入错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.txt_pwd2.Text != this.txt_pwd1.Text)
            {
                MessageBox.Show("2次录入密码不一致", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            _user.Pwd = this.txt_pwd2.Text;

            UserHelper.Instance.Save();
            LogHelper.Instance.ParamChange($"【变更密码】{_user.Name}");
            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
        private void btn_canel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_touchId_Click(object sender, EventArgs e)
        {
            int id = DBHelper.Instance.Sql.Queryable<TB_UserTouch>().Max(p => p.Id) + 1;
            ZKTHelper.Instance.Fid = id;
            TouchId = string.Empty;
            ZKTHelper.Instance.Enroll();
        }

        private void FrmPwdChange_FormClosing(object sender, FormClosingEventArgs e)
        {
            ZKTHelper.Instance.ImageChangedEvent -= ImageChangedEvent;
            ZKTHelper.Instance.MessageChangeEvent -= MessageChangeEvent;
        }

        private void ImageChangedEvent(Bitmap bitmap)
        {
            picFPImg.Image = bitmap;
        }

        private void MessageChangeEvent(string message)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    this.lab_text.Text = message;

                    if (message == "注册成功")
                    {
                        TouchId = ZKTHelper.Instance.TouchId;
                    }
                }));
            }
        }

        private void btn_saveTouch_Click(object sender, EventArgs e)
        {
            //指纹采集
            if (string.IsNullOrEmpty(TouchId))
            {
                MessageBox.Show("未录入指纹信息?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                return;
            }

            TB_UserTouch userTouch = new TB_UserTouch()
            {
                Role = (int)_user.UserType,
                TouchId = TouchId,
                UserId = _user.Id,
            };
            DBHelper.Instance.Sql.Insertable(userTouch).ExecuteCommand();

            TouchId = string.Empty;
            this.lab_text.Text = "保存成功";
        }
    }
}
